<?php
/**
 * 访问日志统计
 *
 * @author 孙晓晔
 * @version $Id: LogAccess.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/../AdminUtil.class.php');
require_once APP_LIB . 'geoiploc.php';

class LogAccess extends AdminUtil {
	public $tbl = Constant::tbl_log_access;

	public $groupby = array(
		'-' => '请选择',
		'uri' => '模块',
		'action' => '行为',
		'ip' => 'IP',
		'2' => '日期',
		'3' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'uri' => '模块',
		'action' => '操作',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);

		$this->dsn = 'test';
		$this->dsn_r = 'test';
		$this->dsn_w = 'test';
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {

		$controller->display($request, 'logaccess');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		// $controller->assign('t1', strtotime('-1 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/access/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {

		$cc_s = $request['cc_s'];
		$action_s = $request['action_s'];
		$ip_s = $request['ip_s'];
		
		$name_s = $request['name_s'];

		$where = " WHERE 1=1";
		if (!empty($cc_s) && $cc_s <> '-') {
			$where .= " AND uri LIKE '%" . $cc_s . "%'";
		}

		if (!empty($action_s)) {
			$where .= " AND action LIKE '%" . $action_s . "%'";
		}
		
		if (!empty($name_s)) {
			$s = "%" . $name_s . "%";
			$where .= sprintf(" AND (ua LIKE '%s' OR content LIKE '%s') ", $s, $s);
		}

		if (!empty($ip_s)) {
			$where .= " AND ip LIKE '" . $ip_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$t1_s = $request['t1_s'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->_tbl($t1_s), $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['ip'] = sprintf("%s [%s]", getCountryFromIP($row['ip'], "NamE"), $row['ip']);
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/access/list');
		return true;
	}

	function _tbl($t1) {
		if (empty($t1)) {
			return $this->tbl;
		}
		
		$dd = date("Y-m-d");
		
		if ($t1 == $dd) {
			return $this->tbl;
		}

		$dd = date("Ymd", strtotime($t1));
		return sprintf("%s_%s", $this->tbl, $dd);
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);
	
		$row = $this->_view($request['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'log/access/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-7 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/access/group');
		return true;
	}
	
	function groupPara($controller, $request) {
		parent::groupPara($controller, $request);
		
		$t1_s = $request['t1_s'];
		
		$this->tbl = $this->_tbl($t1_s);
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'action');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {

		if ($px == 'ip') {			
			$row['a'] = sprintf("%s [%s]", getCountryFromIP($row['a'], "NamE"), $row['a']);
		} else {
		
			$row = $this->_name_t($row, $px);
		}
		
		return $row;
	}
	
	function _mac($controller, $request) {
	
		$dd = date("Ymd");
		$tbl = sprintf("%s_%s", $this->tbl, $dd);
		
		require_once(dirname(__FILE__) . '/../wifi/UserMac.class.php');
		$aa = new UserMac();
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE action = 'User.login'";
		// $fmt = "SELECT * FROM %s WHERE action = 'User.login' AND id = '64081'";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			// print_r($row);
			if (empty($row['content'])) {
				echo "1";
				continue;
			}
			
			$a = json_decode($row['content']);
			// print_r($a);
			if (empty($a->mac)) {
				echo "2";
				continue;
			}
			
			if ($this->_ua_null($row['ua'])) {
				echo "3";
				continue;
			}
			
			$aa->_update($a->mac, $row['ua']);
		}
	}
	
	function _ua_null($s) {
		if (empty($s)) {
			return true;
		}
		
		if (preg_match("/^(Apache)/i", $s)) {
			return true;
		}
		
		return false;
	}

	function backup($controller, $request) {
		
		$this->_mac($controller, $request);
		
		$dd = date("Ymd", strtotime("-1 day"));
		$tbl = sprintf("%s_%s", $this->tbl, $dd);

		$db = newdb($this->dsn_w);
		$fmt = "ALTER TABLE %s RENAME TO %s";
		$sql = sprintf($fmt, $this->tbl, $tbl);
		$db->query($sql);
		
		$fmt = "CREATE TABLE %s LIKE %s";
		$sql = sprintf($fmt, $this->tbl, $tbl);
		$db->query($sql);
		
		$tbl = ltrim($this->tbl, 'test.');
		$tbl = sprintf("%s_%s", $tbl, date("Ymd", strtotime('-7 day')));
		$fmt = "DROP TABLE IF EXISTS `%s`";
		$sql = sprintf($fmt, $tbl);
		$db->query($sql);
	}
		
	function tj($controller, $request) {
		$bb = $this->_list_email('311');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '网站访问情况';
		
		$aa = array(
			'11'=>array('title'=>'按网页', 'result_s' => "20", 'groupby_s' => "uri"),	
			'12'=>array('title'=>'按用户', 'result_s' => "30", 'groupby_s' => "ip"),
		);
		
		$dd = date("Ymd", strtotime("-1 day"));
		$this->tbl = sprintf("%s_%s", $this->tbl, $dd);
		
		$data = array();
		while (list($k, $v) = each($aa)) {
			$this->result_s = $v['result_s'];
			$this->groupby_s = $v['groupby_s'];
			$row = $this->_tj($controller, $request);
			
			$row['title'] = $v['title'];
			
			$data[] = $row;
		}
				
		$controller->assign_by_ref('result', $data);

		$body = $controller->sunxy($request, 'log/access/tj_list');
		
		while (list($k, $to) = each($bb)) {
			// $to = '997264@qq.com';
			echo $to . "\n";
			$this->mail_html($from, $to, $subject, $body);
		}

		logdate($controller, $request);
		return true;
	}
		
	function _tj($controller, $request) {	
		$s = $this->groupby($controller, $this->groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$fmt = "SELECT %s as a, COUNT(%s) as c FROM %s a WHERE createtime > %s GROUP BY %s ORDER BY c DESC";
		$sql = sprintf($fmt, $s, $s, $this->tbl, $df, $s);
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function _test($db, $dd) {
		$tbl = ltrim($this->tbl, 'test.');
		
		$tbl = sprintf("%s_%s", $tbl, $dd);
		$fmt = "DROP TABLE IF EXISTS `%s`";
		$sql = sprintf($fmt, $tbl);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function t($controller, $request) {
		
		$db = newdb("abc");
		$i = 60;
		while ($i > 7) {
			$dd = date("Ymd", strtotime("-$i day"));
			$this->_test($db, $dd);
			
			$i--;
		}
		
		logdate($controller, $request);
		return true;
	}
}
?>